package org.odk.collect.android.fragments;

import android.app.Fragment;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.odk.collect.android.R;
import org.odk.collect.android.adapters.FormListAdapter;
import org.odk.collect.android.adapters.ProjectListAdapter;
import org.odk.collect.android.model.Project;
import org.odk.collect.android.provider.InstanceProviderAPI;

public class FormListFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.frg_form_list, container, false);
        return inflater.inflate(R.layout.frg_form_list, container, false); //chooser_list_layout

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // create an adapter & assign it to list
//        ArrayAdapter adapter = new FormListAdapter(getActivity().getApplicationContext(), Project.getDemoProjectList());
//        ListView lv = (ListView)getView().findViewById(R.id.lst_forms);
//        lv.setAdapter(adapter);

        String selection = InstanceProviderAPI.InstanceColumns.STATUS + " != ?";
        String[] selectionArgs = {InstanceProviderAPI.STATUS_SUBMITTED};
        Cursor c = getActivity().managedQuery(InstanceProviderAPI.InstanceColumns.CONTENT_URI, null, selection, selectionArgs, InstanceProviderAPI.InstanceColumns.STATUS + " desc");

        String[] data = new String[] {
                InstanceProviderAPI.InstanceColumns.DISPLAY_NAME, InstanceProviderAPI.InstanceColumns.DISPLAY_SUBTEXT
        };
        int[] view = new int[] {
                R.id.text1, R.id.text2
        };

        // render total instance view
        SimpleCursorAdapter instances =
                new SimpleCursorAdapter(getActivity(), R.layout.lst_item_form, c, data, view); //two_item
        ListView lv = (ListView)getView().findViewById(android.R.id.list);
        lv.setOnItemClickListener(new OnFormListItemClick());
        lv.setAdapter(instances);
    }

    class OnFormListItemClick implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long l) {
            Cursor c = (Cursor) listView.getAdapter().getItem(position);
            getActivity().startManagingCursor(c);
            Uri instanceUri =
                    ContentUris.withAppendedId(InstanceProviderAPI.InstanceColumns.CONTENT_URI,
                            c.getLong(c.getColumnIndex(InstanceProviderAPI.InstanceColumns._ID)));

            String action = getActivity().getIntent().getAction();
            if (Intent.ACTION_PICK.equals(action)) {
                // caller is waiting on a picked form
                getActivity().setResult(getActivity().RESULT_OK, new Intent().setData(instanceUri));
            } else {
                // the form can be edited if it is incomplete or if, when it was
                // marked as complete, it was determined that it could be edited
                // later.
                String status = c.getString(c.getColumnIndex(InstanceProviderAPI.InstanceColumns.STATUS));
                String strCanEditWhenComplete =
                        c.getString(c.getColumnIndex(InstanceProviderAPI.InstanceColumns.CAN_EDIT_WHEN_COMPLETE));

                boolean canEdit = status.equals(InstanceProviderAPI.STATUS_INCOMPLETE)
                        || Boolean.parseBoolean(strCanEditWhenComplete);
                if (!canEdit) {
//                getActivity().createErrorDialog(getString(R.string.cannot_edit_completed_form),
//                        getActivity().DO_NOT_EXIT);
                    Toast.makeText(getActivity(), getString(R.string.cannot_edit_completed_form), Toast.LENGTH_SHORT).show();
                    return;
                }
                // caller wants to view/edit a form, so launch formentryactivity
                startActivity(new Intent(Intent.ACTION_EDIT, instanceUri));
            }
//        finish();
        }
    }
    protected void onListItemClick(ListView listView, View view, int position, long id) {
        Cursor c = (Cursor) listView.getAdapter().getItem(position);
        getActivity().startManagingCursor(c);
        Uri instanceUri =
                ContentUris.withAppendedId(InstanceProviderAPI.InstanceColumns.CONTENT_URI,
                        c.getLong(c.getColumnIndex(InstanceProviderAPI.InstanceColumns._ID)));

        String action = getActivity().getIntent().getAction();
        if (Intent.ACTION_PICK.equals(action)) {
            // caller is waiting on a picked form
            getActivity().setResult(getActivity().RESULT_OK, new Intent().setData(instanceUri));
        } else {
            // the form can be edited if it is incomplete or if, when it was
            // marked as complete, it was determined that it could be edited
            // later.
            String status = c.getString(c.getColumnIndex(InstanceProviderAPI.InstanceColumns.STATUS));
            String strCanEditWhenComplete =
                    c.getString(c.getColumnIndex(InstanceProviderAPI.InstanceColumns.CAN_EDIT_WHEN_COMPLETE));

            boolean canEdit = status.equals(InstanceProviderAPI.STATUS_INCOMPLETE)
                    || Boolean.parseBoolean(strCanEditWhenComplete);
            if (!canEdit) {
//                getActivity().createErrorDialog(getString(R.string.cannot_edit_completed_form),
//                        getActivity().DO_NOT_EXIT);
                Toast.makeText(getActivity(), getString(R.string.cannot_edit_completed_form), Toast.LENGTH_SHORT).show();
                return;
            }
            // caller wants to view/edit a form, so launch formentryactivity
            startActivity(new Intent(Intent.ACTION_EDIT, instanceUri));
        }
//        finish();
    }    
}
